<template>
	<xn-form-container
		:title="formData.id ? '编辑任务信息' : '增加任务信息'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="任务名称：" name="taskName">
						<a-input v-model:value="formData.taskName" placeholder="请输入任务名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="任务活动类型：" name="acTypeId">
						<a-select v-model:value="formData.acTypeId" placeholder="请选择任务活动类型" :options="acTypeIdOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="任务状态：" name="status">
						<a-select v-model:value="formData.status" placeholder="请选择任务状态" :options="statusOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="计划开始时间：" name="planStartTime">
						<a-date-picker v-model:value="formData.planStartTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择计划开始时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="计划结束时间：" name="planEndTime">
						<a-date-picker v-model:value="formData.planEndTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择计划结束时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="实际开始时间：" name="realStartTime">
						<a-date-picker v-model:value="formData.realStartTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择实际开始时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="实际结束时间：" name="realEndTime">
						<a-date-picker v-model:value="formData.realEndTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择实际结束时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="计划工作量：" name="planWorkload">
						<a-input v-model:value="formData.planWorkload" placeholder="请输入计划工作量" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="实际工作量：" name="realWorkload">
						<a-input v-model:value="formData.realWorkload" placeholder="请输入实际工作量" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="父任务ID：" name="parentId">
						<a-input v-model:value="formData.parentId" placeholder="请输入父任务ID" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12"  v-show="!qoneProjectId">
					<a-form-item label="项目ID：" name="projectId">
						<a-input v-model:value="formData.projectId" placeholder="请输入项目ID" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="任务负责人：" name="principal">
						<a-input v-model:value="formData.principal" placeholder="请输入任务负责人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="扩展信息：" name="extJson">
						<a-input v-model:value="formData.extJson" placeholder="请输入扩展信息" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="密级：" name="secLevel">
						<a-input v-model:value="formData.secLevel" placeholder="请输入密级" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="qoneTaskInfoForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'

	import qoneTaskInfoApi from '@/api/pm/qoneTaskInfoApi'
	import {globalStore} from "@/store";
	const store = globalStore()
	const qoneProjectId = store.projectId
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const acTypeIdOptions = ref([])
	const statusOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		if (record) {
			console.log(record.Id)
		}else{

		}
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}else {

			formData.value.projectId = qoneProjectId
		}
		acTypeIdOptions.value = tool.dictList('TASK_AC_TYPE')
		statusOptions.value = tool.dictList('TASK_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			qoneTaskInfoApi
				.qoneTaskInfoSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
